// pages/real/makeAtlas/index.js
const app = getApp()
const utils = require('../../../utils/util.js')
const doublebed='../../../static/img/atlas/doublebed.png'
const table='../../../static/img/atlas/table.png'
const singlebed='../../../static/img/atlas/singlebed.png'
const toilet='../../../static/img/atlas/toilet.png'
const cabinet='../../../static/img/atlas/cabinet.png'
const wardrobe='../../../static/img/atlas/wardrobe.png'
const door='../../../static/img/atlas/door.png'
const balcony='../../../static/img/atlas/balcony.png'
const modules='../../../static/img/atlas/module.png'
const firewall='../../../static/img/atlas/firewall.png'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pictureIndex:1,
    showPrivacyPopup:false,
    isIphoneX: app.globalData.isIphoneX,
    navTop:wx.getMenuButtonBoundingClientRect().top,
    menuHeight:app.globalData.menuHeight,  
    navHeight: app.globalData.navHeight,
    bigImgUrls:[],
    capBarMoveFlag:false,
    scrollCurrent:0,
    iconList:{
     '202':doublebed ,
     '201':table,
     '204':singlebed,
     '209':toilet,
     '206':cabinet,
     '208':wardrobe,
     '200':door,
     '210':firewall,
     '205':modules,
     '207':balcony
  },
    spaceList:[
      // {
      //   id:1,
      //   title:'书房',
      //   flag:true,
      //   url:'https://t7.baidu.com/it/u=4162611394,4275913936&fm=193&f=GI'
      // },
      // {
      //   id:2,
      //   title:'客厅',
      //   flag:false,
      //   url:'https://t7.baidu.com/it/u=4162611394,4275913936&fm=193&f=GI'
      // },
      // {
      //   id:3,
      //   title:'餐厅',
      //   flag:false,
      //   url:'https://t7.baidu.com/it/u=4162611394,4275913936&fm=193&f=GI'
      // },
      // {
      //   id:4,
      //   title:'主卧',
      //   flag:false,
      //   url:'https://t7.baidu.com/it/u=4162611394,4275913936&fm=193&f=GI'
      // },
      // {
      //   id:5,
      //   title:'阳台',
      //   flag:false,
      //   url:'https://t7.baidu.com/it/u=4162611394,4275913936&fm=193&f=GI'
      // },
      // {
      //   id:6,
      //   title:'主卧',
      //   flag:false,
      //   url:'https://t7.baidu.com/it/u=4162611394,4275913936&fm=193&f=GI'
      // },
      // {
      //   id:7,
      //   title:'阳台',
      //   flag:false,
      //   url:'https://t7.baidu.com/it/u=4162611394,4275913936&fm=193&f=GI'
      // }
    ],
    imgList:[
      {
        id:1,
        url:'https://t7.baidu.com/it/u=4162611394,4275913936&fm=193&f=GI',
      },
      {
        id:2,
        url:'https://t7.baidu.com/it/u=4162611394,4275913936&fm=193&f=GI',
      },
      {
        id:3,
        url:'https://t7.baidu.com/it/u=4162611394,4275913936&fm=193&f=GI',
      },
      {
        id:4,
        url:'https://t7.baidu.com/it/u=4162611394,4275913936&fm=193&f=GI',
      },
      {
        id:5,
        url:'https://t7.baidu.com/it/u=4162611394,4275913936&fm=193&f=GI',
      }
    ],
    scene:"",
    detailList:{},
    detailId:"",
    tagList:[],
    userInfo:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const userInfo = wx.getStorageSync("userInfo")
    if(userInfo){
      this.setData({showPrivacyPopup:true})
    }
    const phone = wx.getStorageSync('phone')
   
    const {  id } = options
    const scene = options.scene || null
      // 获取用户信息
    this.setData({ userInfo, phone, detailId:id, scene })  
    this.initData()
  },
  initData() { 
    if(!this.data.userInfo) {
      this.toLoginPage()
    } else {
      this.initDetailList()
    }
    setTimeout(()=>{
      if(!this.data.userInfo) {
        wx.hideShareMenu({
          menus: ['shareAppMessage', 'shareTimeline']
        })
      }
    },200)
  },
  initDetailList() {
    if(this.data.scene) {
      const unionid = wx.getStorageSync("unionid")
      const str = { code: this.data.scene, createBy: unionid }
      this.hendleGetCodeList(str)
    }else{
      this.handleGetDetialList()
    }
  },
  /**
   * 获取本地用户信息
  */
  getLocalUserInfo: function() {
    const userInfo = wx.getStorageSync('userInfo')

    if (userInfo) {
      this.setData({ userInfo, phone: userInfo.phone })
    } 
    this.initData()
    // else {
    //   this.toLoginPage()
    // }
  },
  selectSpaceItem(e){
    // console.log(e)
    const item=e.currentTarget.dataset.item
    const imgList=item.realityImageList
    const {spaceList,detailList}=this.data
   const list= spaceList.map(s=>{
     if(s.id===item.id){
       s.flag=true
     }else{
       s.flag=false
     }
     return s
   })
   detailList.realityImageList=imgList
   this.setData({
     spaceList:list,
     detailList,
     pictureIndex:1
   })
  },
  toLoginPage(){
    const {userInfo , phone,scene } = this.data
    let message = ''
    if(!userInfo) {
      message = '未登陆，是否跳到登陆页登陆'
    }
    const { detailId } = this.data
    wx.showModal({
      title: '提示',
      content: message,
      success:function(res) {
        if(res.confirm) {
          wx.navigateTo({
            url: '/pages/mine/register/index'
          });
        }else if (res.cancel) {
          wx.switchTab({
            url: '/pages/real/homepage/index'
          });
        }
      }
    })
  },
  hendleGetCodeList(str){
    utils.$http(app.globalData.api.getRealContriGetByCode, str, 'POST', true, 'JSON').then(res => {
      if(res.success){
        const s = res.data.content
        let imgList=res.data.realityImageList.map(i=>{
          const u=utils.filterURL(i.picUrl)
          return u
        })
        let newSpace=[]
        if(res.data.contributeType==='2' && res.data.spaceDTOList && res.data.spaceDTOList.length){
          imgList=res.data.spaceDTOList[0].realityImageList.map(i=>{
            const u=utils.filterURL(i.picUrl)
            return u
          })
          res.data.realityImageList=res.data.spaceDTOList[0].realityImageList
           newSpace=res.data.spaceDTOList.map(item=>{
            for(let i in this.data.iconList){
              if(i===item.spaceCode){
                item.iconUrl=this.data.iconList[i]
              }
            }
            return item
          })
          res.data.realityImageList=res.data.spaceDTOList[0].realityImageList
        }
        this.setData({
         detailList:res.data,
         tagList:s.slice(0,-1).split('.').map((item,index)=>{
           const str={
             id:index,
             flag:false,
             name:item.replace(/,/g, ' , ')
           }
           return str
         }),
         bigImgUrls:imgList,
         spaceList:newSpace
       })
      }
    })
  },
  showPreView(e){
    const {bigImgUrls} =this.data
    const {src} =e.currentTarget.dataset
    wx.previewImage({
      urls: bigImgUrls,
      showmenu:false,
      current:utils.filterURL(src)
    })
  },
  handleGetDetialList(){
    const userInfo=wx.getStorageSync('userInfo')
    const unionid=wx.getStorageSync("unionid")
    const str={
      id:this.data.detailId,
      createBy:unionid
    }
    utils.$http(app.globalData.api.getRealContribDetail, str, 'POST', true, 'JSON').then(res => {
      if(res.success){
        const s= res.data.content
        let imgList=res.data.realityImageList.map(i=>{
          const u=utils.filterURL(i.picUrl)
          return u
        })
        let newSpace=[]
        if(res.data.contributeType==='2' && res.data.spaceDTOList && res.data.spaceDTOList.length){
          imgList=res.data.spaceDTOList[0].realityImageList.map(i=>{
            const u=utils.filterURL(i.picUrl)
            return u
          })
           newSpace=res.data.spaceDTOList.map((item,index)=>{
             item.flag=false
             if(index===0) item.flag=true
            for(let i in this.data.iconList){
              if(i===item.spaceCode){
                item.iconUrl=this.data.iconList[i]
              }
            }
            return item
          })
          res.data.realityImageList=res.data.spaceDTOList[0].realityImageList
        }
           this.setData({
            detailList:res.data,
            tagList:s.slice(0,-1).split('.').map((item,index)=>{
              const str={
                id:index,
                flag:false,
                name:item.replace(/,/g, ' , ')
              }
              return str
            }),
            bigImgUrls:imgList,
            spaceList:newSpace
          })
          // console.log(this.data.bigImgUrls)
          // console.log(this.data.spaceList)
      }
    }) 
  },
// 获取当前轮播图的index
onSlideChangeEnd: function (e) {
  var that = this;
  const {detailList={}}=that.data
  const len=detailList.realityImageList.length
  const index=len&&e.detail.current<=len-1?e.detail.current + 1:1
  that.setData({
    pictureIndex: index
  })
},
onClickLeft(){
    wx.navigateBack({

    })
  
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  onPageScroll(e){
    const isSatisfy = e.scrollTop  >= 60  // 高度大于100的时候 显示白底标签栏
    if(this.data.capBarMoveFlag == isSatisfy) { 
      return false;
    } else {
      this.setData({
        capBarMoveFlag: isSatisfy
      })
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const sharerPhone = wx.getStorageSync('phone') || ''
    const sharerOpenId = wx.getStorageSync('openId') 

    const imageUrl = this.data.detailList.realityPic && utils.filterURL(this.data.detailList.realityPic) 

    return {
      title: this.data.detailList.realityName || '详情页',
      imageUrl: imageUrl,
      path: "pages/real/makeAtlas/index?id=" + this.data.detailList.id + "&sharerPhone=" + sharerPhone + "&sharerOpenId=" + sharerOpenId
    }
  }
})